<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template name="get_search_item">
    <xsl:variable name="display_length" select="64" />
    <xsl:choose>
        <xsl:when test="string(./GroupName) = 'Document'">
            <em>Title:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='Title'],./SearchString) and string-length(./SearchResults/SearchResult[@name='Title']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='Title'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='Title'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='Title'],./SearchString) and string-length(./SearchResults/SearchResult[@name='Title']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='Title'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='Title'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='Title']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='Title'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='Title']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='Title']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>Document No.:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='DocumentShortName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='DocumentShortName']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='DocumentShortName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='DocumentShortName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='DocumentShortName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='DocumentShortName']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='DocumentShortName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='DocumentShortName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='DocumentShortName']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='DocumentShortName'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='DocumentShortName']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='DocumentShortName']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>File Name:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='FileName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='FileName']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='FileName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='FileName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='FileName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='FileName']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='FileName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='FileName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='FileName']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='FileName'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='FileName']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='FileName']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>Path:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='Path'],./SearchString) and string-length(./SearchResults/SearchResult[@name='Path']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='Path'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='Path'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='Path'],./SearchString) and string-length(./SearchResults/SearchResult[@name='Path']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='Path'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='Path'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='Path']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='Path'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='Path']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='Path']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>Comments:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='Tag'],./SearchString) and string-length(./SearchResults/SearchResult[@name='Tag']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='Tag'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='Tag'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='Tag'],./SearchString) and string-length(./SearchResults/SearchResult[@name='Tag']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='Tag'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='Tag'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='Tag']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='Tag'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='Tag']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='Tag']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>Keywords:</em>&#160;
            <xsl:choose>
                <xsl:when test="./SearchResults/SearchResult/Keywords">
                <xsl:for-each select="./SearchResults/SearchResult/Keywords">
                    <xsl:choose>
                        <xsl:when test="position() &gt; 1">
                            <xsl:text>, </xsl:text><xsl:value-of select="./Keyword" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="./Keyword" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>[None]</xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:when test="string(./GroupName) = 'Fragment'">
            <em>Name:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='FragmentName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='FragmentName']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='FragmentName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='FragmentName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='FragmentName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='FragmentName']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='FragmentName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='FragmentName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='FragmentName']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='FragmentName'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='FragmentName']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='FragmentName']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>Display Name:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='FragmentDisplayName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='FragmentDisplayName']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='FragmentDisplayName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='FragmentDisplayName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='FragmentDisplayName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='FragmentDisplayName']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='FragmentDisplayName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='FragmentDisplayName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='FragmentDisplayName']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='FragmentDisplayName'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='FragmentDisplayName']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='FragmentDisplayName']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>Prose (short):</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='ItemChar'],./SearchString) and string-length(./SearchResults/SearchResult[@name='ItemChar']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='ItemChar'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='ItemChar'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='ItemChar'],./SearchString) and string-length(./SearchResults/SearchResult[@name='ItemChar']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='ItemChar'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='ItemChar'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='ItemChar']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='ItemChar'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='ItemChar']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='ItemChar']" />
                </xsl:otherwise>
            </xsl:choose>
            <br />
            <em>Prose (long):</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='ItemText'],./SearchString) and string-length(./SearchResults/SearchResult[@name='ItemText']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='ItemText'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='ItemText'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='ItemText'],./SearchString) and string-length(./SearchResults/SearchResult[@name='ItemText']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='ItemText'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='ItemText'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='ItemText']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='ItemText'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='ItemText']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='ItemText']" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:when test="string(./GroupName) = 'Segment'">
            <em>Name:</em>&#160;
            <xsl:choose>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='SegmentName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='SegmentName']) &lt;= $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='SegmentName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='SegmentName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    <xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />
                </xsl:when>
                <xsl:when test="contains(./SearchResults/SearchResult[@name='SegmentName'],./SearchString) and string-length(./SearchResults/SearchResult[@name='SegmentName']) &gt; $display_length">
                    <xsl:variable name="s_after" select="substring-after(./SearchResults/SearchResult[@name='SegmentName'],./SearchString)" />
                    <xsl:variable name="s_before" select="substring-before(./SearchResults/SearchResult[@name='SegmentName'],./SearchString)" />
                    <xsl:variable name="s_before_length" select="string-length($s_before)" />
                    …<xsl:value-of select="substring($s_before,number($s_before_length - $display_length))" /><strong><xsl:value-of select="./SearchString" /></strong><xsl:value-of select="substring($s_after,1,$display_length)" />…
                </xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='SegmentName']) &gt; $display_length"><xsl:value-of select="substring(./SearchResults/SearchResult[@name='SegmentName'],1,$display_length)" />…</xsl:when>
                <xsl:when test="string-length(./SearchResults/SearchResult[@name='SegmentName']) = 0">[None]</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="./SearchResults/SearchResult[@name='SegmentName']" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet> 
